package com.cz.influxdb.test;

import cn.hutool.json.JSONUtil;
import com.cz.influxdb.config.InfluxDBHelper;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;

import java.util.List;

public class QueryWeather {
    public static void main(String[] args) {
        QueryWeather queryWeather = new QueryWeather();
        queryWeather.query1();
    }

    public void query1() {
        InfluxDBHelper influxDBHelper = new InfluxDBHelper();
        InfluxDBClient influxDBClient = influxDBHelper.getInfluxDBClient();
        String fluxQuery = "from(bucket: \"root\")\n" +
                "  |> range(start: -1y) // 查询最近1小时的数据\n" +
                "  |> filter(fn: (r) => r._measurement == \"weather\")" +
                "  |> filter(fn: (r) => r._field == \"humidity\")";
        List<WeatherEntity> dataList = influxDBClient.getQueryApi().query(fluxQuery, WeatherEntity.class);
        // 循环遍历查询结果
        for (WeatherEntity fluxTable : dataList) {
            System.out.println(JSONUtil.toJsonStr(fluxTable));
        }
        influxDBClient.close();
    }

    public void query2() {
        InfluxDBHelper influxDBHelper = new InfluxDBHelper();
        InfluxDBClient influxDBClient = influxDBHelper.getInfluxDBClient();
        String fluxQuery = "from(bucket: \"root\")\n" +
                "  |> range(start: -inf) // 查询最近1小时的数据\n" +
                "  |> filter(fn: (r) => r._measurement == \"weather\")" +
                "  |> filter(fn: (r) => r._field == \"temperature\")" +
                "  |> sum()";
        List<FluxTable> dataList = influxDBClient.getQueryApi().query(fluxQuery);
        // 循环遍历查询结果
        for (FluxTable fluxTable : dataList) {
            List<FluxRecord> records = fluxTable.getRecords();
            for (FluxRecord record : records) {
                System.out.println("Time: " + record.getTime() + ",Key:" + record.getField() + ", Value: " + record.getValue());
                // 你可以在这里根据需要处理每一行数据
            }
        }
        influxDBClient.close();
    }

    public void query3() {
        InfluxDBHelper influxDBHelper = new InfluxDBHelper();
        InfluxDBClient influxDBClient = influxDBHelper.getInfluxDBClient();
        String fluxQuery = "from(bucket: \"root\")\n" +
                "  |> range(start: 0) // 查询最近1小时的数据\n" +
                "  |> filter(fn: (r) => r._measurement == \"weather\")" +
                "  |> filter(fn: (r) => r._field == \"temperature\")" +
                "  |> filter(fn: (r) => r.location == \"上海\")"+
                "  |> median()";
        List<FluxTable> dataList = influxDBClient.getQueryApi().query(fluxQuery);
        // 循环遍历查询结果
        for (FluxTable fluxTable : dataList) {
            List<FluxRecord> records = fluxTable.getRecords();
            for (FluxRecord record : records) {
                System.out.println("Time: " + record.getTime() + ",Key:" + record.getField() + ", Value: " + record.getValue());
                // 你可以在这里根据需要处理每一行数据
            }
        }
        influxDBClient.close();
    }
}
